home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 23 / AMIGAplus Sonderheft 23 (2000)(Falke)(DE)[!].iso / Updates / Datatypes / RGFX-DT / Source / savergfx.c < prev   
C/C++ Source or Header  |  1999-11-14  |  6KB  |  230 lines

  1. #include <clib/alib_protos.h>
  2. #include <clib/cybergraphics_protos.h>
  3. #include <clib/datatypes_protos.h>
  4. #include <clib/exec_protos.h>
  5. #include <clib/graphics_protos.h>
  6. #include <clib/iffparse_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/xpkmaster_protos.h>
  9. #include <datatypes/pictureclass.h>
  10. #include <exec/memory.h>
  11. //#include <libraries/rgfx.h>
  12. #include "class.h"
  13. #include "rgfx.h"
  14.  
  15. extern Library *CyberGfxBase;
  16.  
  17. void ReadPrefs(Data *data);
  18.  
  19. static BOOL PutRGHD(IFFHandle *iff,RGHD *rh)
  20. {
  21. BOOL ret=0;
  22. if(!PushChunk(iff,0,ID_RGHD,sizeof(RGHD)))
  23.     {
  24.     if(WriteChunkRecords(iff,rh,sizeof(RGHD),1)==1)
  25.         {
  26.         if(!PopChunk(iff)) ret=1;
  27.         }
  28.     }
  29. return ret;
  30. }
  31.  
  32. static BOOL PutRCOL(Object *obj,IFFHandle *iff)
  33. {
  34. BOOL ret=0;
  35. if(!PushChunk(iff,0,ID_RCOL,sizeof(RCOL)))
  36.     {
  37.     RCOL *rc;
  38.     if(rc=(RCOL *)AllocVec(sizeof(RCOL),MEMF_CLEAR))
  39.         {
  40.         ULONG i,n,*cr;
  41.         GetDTAttrs(obj,PDTA_NumColors,&n,PDTA_CRegs,&cr,TAG_END);
  42.         for(i=0;i<n;i++)
  43.             {
  44.             UWORD j;
  45.             for(j=0;j<3;j++) rc->rcol_Colors[i][j]=*(cr++)>>24;
  46.             }
  47.         if(WriteChunkRecords(iff,rc,sizeof(RCOL),1)==1)
  48.             {
  49.             if(!PopChunk(iff)) ret=1;
  50.             }
  51.         FreeVec(rc);
  52.         }
  53.     }
  54. return ret;
  55. }
  56.  
  57. static BOOL PutRSCM(Object *obj,IFFHandle *iff,RGHD *rh)
  58. {
  59. BOOL ret=0;
  60. if(!PushChunk(iff,0,ID_RSCM,sizeof(RSCM)))
  61.     {
  62.     ULONG mode;
  63.     RSCM rs={INVALID_ID,INVALID_ID,INVALID_ID};
  64.     GetDTAttrs(obj,PDTA_ModeID,&mode,TAG_END);
  65.     if(CyberGfxBase&&IsCyberModeID(mode))
  66.         {
  67.         rs.rscm_CGfx=mode;
  68.         if(rh->rgfx_Depth<=8) rs.rscm_AGA=BestModeID(
  69.         BIDTAG_NominalWidth,rh->rgfx_PageWidth,BIDTAG_NominalHeight,rh->rgfx_PageHeight,
  70.         BIDTAG_DesiredWidth,rh->rgfx_Width,BIDTAG_DesiredHeight,rh->rgfx_Height,
  71.         BIDTAG_Depth,rh->rgfx_Depth,BIDTAG_MonitorID,DEFAULT_MONITOR_ID,
  72.       BIDTAG_DIPFMustNotHave,DIPF_IS_DUALPF|DIPF_IS_PF2PRI|DIPF_IS_HAM|DIPF_IS_EXTRAHALFBRITE,TAG_END);
  73.         }
  74.     else
  75.         {
  76.         rs.rscm_AGA=mode;
  77.         if(!(mode&(HAM_KEY|EXTRAHALFBRITE_KEY|LORESDPF_KEY)))
  78.             {
  79.             if(CyberGfxBase) rs.rscm_CGfx=BestCModeIDTags(CYBRBIDTG_NominalWidth,rh->rgfx_Width,CYBRBIDTG_NominalHeight,rh->rgfx_Height,CYBRBIDTG_Depth,rh->rgfx_Depth,TAG_END);
  80.             }
  81.         }
  82.     if(WriteChunkRecords(iff,&rs,sizeof(RSCM),1)==1)
  83.         {
  84.         if(!PopChunk(iff)) ret=1;
  85.         }
  86.     }
  87. return ret;
  88. }
  89.  
  90. static BOOL WriteChunky_XPK(Object *obj,IFFHandle *iff,RGHD *rh,BitMap *bm,Data *data)
  91. {
  92. BOOL ret=0;
  93. ULONG len=rh->rgfx_Width*rh->rgfx_Height;
  94. UBYTE *mem;
  95. if(mem=(UBYTE *)AllocVec(len,0))
  96.     {
  97.     UBYTE *pix;
  98.     if(pix=(UBYTE *)AllocVec(((rh->rgfx_Width+16)>>4)<<4,0))
  99.         {
  100.         BitMap *tbm;
  101.         if(tbm=AllocBitMap(rh->rgfx_Width,1,rh->rgfx_Depth,0,bm))
  102.             {
  103.             ULONG y,xpklen,outlen;
  104.             UBYTE *xpk=mem;
  105.             RastPort rp,trp;
  106.             InitRastPort(&rp);
  107.             InitRastPort(&trp);
  108.             rp.BitMap=bm;
  109.             trp.BitMap=tbm;
  110.             for(y=0;y<rh->rgfx_Height;y++,xpk+=rh->rgfx_Width)
  111.                 {
  112.                 ReadPixelLine8(&rp,0,y,rh->rgfx_Width,pix,&trp);
  113.                 CopyMem(pix,xpk,rh->rgfx_Width);
  114.                 }
  115.             if(!XpkPackTags(XPK_InBuf,mem,XPK_InLen,len,XPK_GetOutBuf,&xpk,XPK_GetOutBufLen,&xpklen,XPK_GetOutLen,&outlen,XPK_PackMethod,data->xpk,XPK_PackMode,data->xpkmode,XPK_Password,data->password,TAG_END))
  116.                 {
  117.                 if(WriteChunkRecords(iff,xpk,outlen,1)==1) ret=1;
  118.                 FreeMem(xpk,xpklen);
  119.                 }
  120.             FreeBitMap(tbm);
  121.             }
  122.         FreeVec(pix);
  123.         }
  124.     FreeVec(mem);
  125.     }
  126. return ret;
  127. }
  128.  
  129. static BOOL Write24_XPK(Object *obj,IFFHandle *iff,RGHD *rh,Data *data)
  130. {
  131. BOOL ret=0;
  132. UBYTE *pix;
  133. ULONG len=rh->rgfx_BytesPerLine*rh->rgfx_Height;
  134. if(pix=(UBYTE *)AllocVec(len,0))
  135.     {
  136.     ULONG xpklen,outlen;
  137.     UBYTE *xpk;
  138.     DoMethod(obj,PDTM_READPIXELARRAY,pix,RECTFMT_RGB,rh->rgfx_BytesPerLine,0,0,rh->rgfx_Width,rh->rgfx_Height);
  139.     if(!XpkPackTags(XPK_InBuf,pix,XPK_InLen,len,XPK_GetOutBuf,&xpk,XPK_GetOutBufLen,&xpklen,XPK_GetOutLen,&outlen,XPK_PackMethod,data->xpk,XPK_PackMode,data->xpkmode,XPK_Password,data->password,TAG_END))
  140.         {
  141.         if(WriteChunkRecords(iff,xpk,outlen,1)==1) ret=1;
  142.         FreeMem(xpk,xpklen);
  143.         }
  144.     FreeVec(pix);
  145.     }
  146. return ret;
  147. }
  148.  
  149. static BOOL PutBODY(IClass *cl,Object *obj,IFFHandle *iff,RGHD *rh)
  150. {
  151. BOOL ret=0;
  152. if(!PushChunk(iff,0,ID_RBOD,IFFSIZE_UNKNOWN))
  153.     {
  154.     BOOL wok=0;
  155.     BitMap *bm;
  156.     Data *data=(Data *)INST_DATA(cl,obj);
  157.     ReadPrefs(data);
  158.     GetDTAttrs(obj,PDTA_BitMap,&bm,TAG_END);
  159.     if(rh->rgfx_Depth>8) wok=Write24_XPK(obj,iff,rh,data);
  160.     else wok=WriteChunky_XPK(obj,iff,rh,bm,data);
  161.     if(wok)
  162.         {
  163.         if(!PopChunk(iff)) ret=1;
  164.         }
  165.     }
  166. return ret;
  167. }
  168.  
  169. ULONG SaveRGFX(IClass *cl,Object *obj,dtWrite *msg)
  170. {
  171. ULONG retval=0;
  172. IFFHandle *iff;
  173. SignalSemaphore *lock=((DTSpecialInfo *)((Gadget *)obj)->SpecialInfo);
  174. ObtainSemaphoreShared(lock);
  175. if(iff=AllocIFF())
  176.     {
  177.     iff->iff_Stream=msg->dtw_FileHandle;
  178.     InitIFFasDOS(iff);
  179.     if(!OpenIFF(iff,IFFF_WRITE))
  180.         {
  181.         if(!PushChunk(iff,ID_RGFX,ID_FORM,IFFSIZE_UNKNOWN))
  182.             {
  183.             BOOL ok=0;
  184.             BitMapHeader *bh;
  185.             RGHD rh;
  186.             GetDTAttrs(obj,PDTA_BitMapHeader,&bh,TAG_END);
  187.             rh.rgfx_LeftEdge=bh->bmh_Left;
  188.             rh.rgfx_TopEdge=bh->bmh_Top;
  189.             rh.rgfx_Width=bh->bmh_Width;
  190.             rh.rgfx_Height=bh->bmh_Height;
  191.             rh.rgfx_PageWidth=bh->bmh_PageWidth;
  192.             rh.rgfx_PageHeight=bh->bmh_PageHeight;
  193.             rh.rgfx_Depth=bh->bmh_Depth;
  194.             if(rh.rgfx_Depth<=8)
  195.                 {
  196.                 rh.rgfx_PixelBits=8;
  197.                 rh.rgfx_BytesPerLine=rh.rgfx_Width;
  198.                 rh.rgfx_BitMapType=RMBT_BYTECHUNKY8;
  199.                 }
  200.             else
  201.                 {
  202.                 rh.rgfx_PixelBits=24;
  203.                 rh.rgfx_BytesPerLine=rh.rgfx_Width*3;
  204.                 rh.rgfx_BitMapType=RMBT_3BYTERGB24;
  205.                 }
  206.             rh.rgfx_Compression=RCMT_XPK;
  207.             rh.rgfx_XAspect=bh->bmh_XAspect;
  208.             rh.rgfx_YAspect=bh->bmh_YAspect;
  209.             if(PutRGHD(iff,&rh))
  210.                 {
  211.                 if(bh->bmh_Depth>8||PutRCOL(obj,iff))
  212.                     {
  213.                     if(PutRSCM(obj,iff,&rh)) ok=PutBODY(cl,obj,iff,&rh);
  214.                     }
  215.                 }
  216.             if(ok)
  217.                 {
  218.                 if(PopChunk(iff)) ok=0;
  219.                 else retval=1;
  220.                 }
  221.             }
  222.         CloseIFF(iff);
  223.         }
  224.     FreeIFF(iff);
  225.     }
  226. ReleaseSemaphore(lock);
  227. return retval;
  228. }
  229.  
  230.